package com.bjsxt.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.CareHistory;
import com.bjsxt.domain.CareOrder;
import com.bjsxt.domain.CareOrderItem;
import com.bjsxt.service.CareHistoryService;
import com.bjsxt.vo.AjaxResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 发药控制层
 */
@RestController
@RequestMapping("doctor/handleMedicine")
public class HandleMedicineController {

    @Reference
    private CareHistoryService careHistoryService;

    /**
     * 根据挂号单id查询未支付的处方信息及详情
     *
     * @param regId
     * @return
     */
    @GetMapping("getChargedCareHistoryOnlyMedicinesByRegId/{regId}")
    @HystrixCommand
    public AjaxResult getChargedCareHistoryOnlyMedicinesByRegId(@Validated @PathVariable String regId) {
//        声明返回的map对象
        Map<String, Object> res = new HashMap<>();
//        根据挂号单id查询病例信息
        CareHistory careHistory = this.careHistoryService.queryCareHistoryByRegId(regId);
        if (careHistory == null) {
            return AjaxResult.fail("【" + regId + "】该挂号单没有对应的病例信息，请核对后再查询");
        }
//        放入默认值
        res.put("careHistory", careHistory);
        res.put("careOrders", Collections.EMPTY_LIST);
//        声明一个可以放careOrder的集合
        List<Map<String, Object>> maps = new ArrayList<>();
//        根据病例编号查询处方
        List<CareOrder> careOrders = this.careHistoryService.queryCareOrdersByChId(careHistory.getChId());
        if (careOrders.isEmpty()) {
            return AjaxResult.fail("【" + regId + "】该挂号单没相关的处方信息，请核对后再查询");
        }
        for (CareOrder careOrder : careOrders) {
//            只选择药用处方
            if (careOrder.getCoType().equals(Constants.CO_TYPE_MEDICINES)) {
                Map<String, Object> beanToMap = BeanUtil.beanToMap(careOrder);
                beanToMap.put("careOrderItems", Collections.EMPTY_LIST);
                BigDecimal allAmount = new BigDecimal("0");
//                根据处方id查询未支付的处方详情列表
                List<CareOrderItem> careOrderItems = this.careHistoryService.queryCareOrderItemsByCoId(careOrder.getCoId(), Constants.ORDER_DETAILS_STATUS_1);
//                如果当前处方id查询未支付的处方详情列表为空，则结束当前循环
                if (careOrderItems.isEmpty()) {
                    continue;
                } else {
//                    重新计算总价
                    for (CareOrderItem careOrderItem : careOrderItems) {
                        allAmount = allAmount.add(careOrderItem.getAmount());
                    }
                    beanToMap.put("careOrderItems", careOrderItems);
                    beanToMap.put("allAmount", allAmount);
                    maps.add(beanToMap);
                }
            }
        }
        if (maps.isEmpty()) {
            return AjaxResult.fail("【" + regId + "】该挂号单没已支付的药品处方信息，请核对后再查询");
        } else {
            res.put("careOrders", maps);
            return AjaxResult.success(res);
        }
    }

    /**
     * 发药
     *
     * @param itemIds
     * @return
     */
    @PostMapping("doMedicine")
    @HystrixCommand
    public AjaxResult doMedicine(@RequestBody List<String> itemIds) {
        if (itemIds == null || itemIds.isEmpty()) {
            return AjaxResult.fail("请选择要发药的药品项");
        }
//        执行发药 情况1：库存够，走正常逻辑 情况2：库存不够，返回哪个药品库存不够，只发有库存的商品，其他的让患者自行退费
        String msg = this.careHistoryService.doMedicine(itemIds);
        if (StringUtils.isBlank(msg)) {
            return AjaxResult.success("发药成功");
        } else {
            return AjaxResult.fail(msg);
        }
    }


}
